﻿using NineSeas.Services;
using NineSeas.ViewModels;
using Orchard;
using Orchard.ContentManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace NineSeas.Dashboard.Api
{
    [Authorize]
    public class ChapterController : ApiController
    {
        private readonly IOrchardServices _services;
        private readonly IChapterService _chapterService;
        public ChapterController(IOrchardServices services,
                IChapterService chapterService)
        {
            _services = services;
            _chapterService = chapterService;
        }

        // GET api/<controller>
        public ChapterListViewModel GetByBook(int bookId,
            int pageIndex = 0,
            int pageSize = 10)
        {
            var paging = new PaginationInfo
            {
                CurrentPage = pageIndex,
                PageSize = pageSize
            };

            return new ChapterListViewModel {
                Chapters = _chapterService.GetByBookId(bookId, paging, VersionOptions.Latest)
                                .Select(c => new ChapterSummaryViewModel {
                                    Id = c.Id,
                                    Title = c.Title,
                                    SortOrder = c.SortOrder != null ? c.SortOrder.Value : 0                                    
                                }),
                Paging = paging
            };
        }

        // GET api/<controller>/5
        public ChapterViewModel Get(int id)
        {
            var chapterPart = _chapterService.Get(id, VersionOptions.Latest);
            return chapterPart != null ? new ChapterViewModel
            {
                Id = chapterPart.Id,
                Title = chapterPart.Title,
                SortOrder = chapterPart.SortOrder.HasValue ? chapterPart.SortOrder.Value : 0,
                Content = chapterPart.Content
            } : null;
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}